package com.gree.controller;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import com.gree.utils.Files;
 
import com.gree.utils.MessageID;
import com.gree.utils.StringTool;
import com.gree.entity.CmnC006User;
import javax.servlet.http.HttpSession;
import com.gree.entity.TrsT021RunmoveRecord;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import or.pagination.MyPager;
import org.springframework.stereotype.Controller;
import com.gree.service.ITrsT021RunmoveRecordService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RunRecordController {
	@Autowired
	private ITrsT021RunmoveRecordService recordService;

	@RequestMapping(value = "/record/getRuns")
	public String getList(HttpSession session, HttpServletResponse response,HttpServletRequest request) throws IOException {
		CmnC006User user = (CmnC006User) session.getAttribute("user");
		if (user != null) {
			List<TrsT021RunmoveRecord> recordsList = recordService.getRunmoveRecordArray(user.getUserId());
			MyPager.getPager(recordsList, request);
			return "runs/Import";
		} else {
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<script>");
			out.println("window.open ('" + request.getContextPath()+ "/Views/index.jsp','_top')");
			out.println("</script>");
			out.println("</html>");
			return null;
		}
	}

	@RequestMapping(value = "/record/getRunArray")
	public String getRequest(HttpSession session, HttpServletRequest request,HttpServletResponse response) throws Exception {
		CmnC006User user = (CmnC006User) session.getAttribute("user");
		if (user != null) {
			List<TrsT021RunmoveRecord> recordsList = recordService.getRunmoveRecordArray(user.getUserId());
			MyPager.getPager(recordsList, request);
			return "runs/MyJsp";
		} else {
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<script>");
			out.println("window.open ('" + request.getContextPath()	+ "/Views/index.jsp','_top')");
			out.println("</script>");
			out.println("</html>");
			return null;
		}
	}

	/*** 文件上传并读取，完整版 */
	@RequestMapping(value = "/record/uploadFile", produces = "text/html;charset=utf-8", method = RequestMethod.POST)
	public void importPicFile(String companyId,@RequestParam(value = "fileField", required = false) MultipartFile fileField,HttpSession session, HttpServletResponse response) throws Exception {
		CmnC006User user = (CmnC006User) session.getAttribute("user");
		String result = null;
		if (fileField == null) {
			result = StringTool.getJSONObject("error", MessageID.FILE_DOES_NOT);
		} else {
			result = Files.getFileUpload(companyId.toUpperCase(), user.getLoginId(),user.getUserId(), fileField);
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);// 返回信息
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/record/{infoId}/reset")
	public String editorReset(@PathVariable String infoId, HttpSession session,HttpServletResponse response, HttpServletRequest request)throws IOException {
		CmnC006User user = (CmnC006User) session.getAttribute("user");
		if (user != null) {
			TrsT021RunmoveRecord obj = new TrsT021RunmoveRecord();
			obj.setInfoId(infoId);
			obj.setHoldUser(user.getUserId());
			recordService.editorReset(obj);
			return "redirect:/record/getRuns";
		} else {
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<script>");
			out.println("window.open ('" + request.getContextPath()+ "/Views/index.jsp','_top')");
			out.println("</script>");
			out.println("</html>");
			return null;
		}
	}

}
